#!/bin/bash

#目录不存在则创建
if [ ! -d "/opt/job" ]; then
    
    mkdir -p /opt/job
fi

#将/cleanup-scripts目录复制到/opt/job
cp -rf /cleanup-scripts /opt/job

# 检复制成功后删除
if [ $? -eq 0 ]; then
  rm -rf /cleanup-scripts
else
  echo "复制失败，请检查权限或其他错误。"
  exit 
fi

#添加定时任务

(crontab -l ; echo "05 07,13,19,01 * * * sh /opt/job/cleanup-scripts/clean-host-cache.sh >> /opt/job/cleanup-scripts/logs/clean-host-cache.log") | crontab -

(crontab -l ; echo "00 01 * * * sh /opt/job/cleanup-scripts/clean-common-logs.sh  >> /opt/job/cleanup-scripts/logs/clean-common-logs.log") | crontab -


# 获取当前用户的Crontab列表
crontab_list=$(crontab -l)

# 将Crontab列表中的任务按行进行排序并去重
unique_crontab=$(echo "$crontab_list" | awk '!x[$0]++')

# 将去重后的任务重新设置为当前用户的Crontab
echo "$unique_crontab" | crontab -


systemctl enable crond
systemctl restart crond

echo "任务列表:"
crontab -l

